package com.nineclock.attendance.controller;

import com.nineclock.attendance.dto.AttendGroupDTO;
import com.nineclock.attendance.dto.MakeupRuleDTO;
import com.nineclock.attendance.dto.OverTimeRuleDTO;
import com.nineclock.attendance.pojo.AtCompanyCommonSetting;
import com.nineclock.attendance.service.AttendGroupService;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping
@Api(value = "考勤组管理接口", tags = "考勤服务")
public class AttendGroupController {

    @Autowired
    private AttendGroupService attendGroupService;

    /**
     * 考勤组: 分页查询
     * 接口路径：GET/atte/attendGroup
     */
    @GetMapping("attendGroup")
    @ApiOperation("考勤组: 分页查询")
    public Result<PageResult<AttendGroupDTO>> queryAttendGroupPage(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<AttendGroupDTO> pageResult = attendGroupService.queryAttendGroupPage(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 考勤组: 添加
     * 接口路径：POST/atte/attendGroup
     */
    @PostMapping("attendGroup")
    @ApiOperation("考勤组: 添加")
    public Result addAttendGroup(@RequestBody AttendGroupDTO attendGroupDTO){
        attendGroupService.addAttendGroup(attendGroupDTO);
        return Result.success();
    }

    /**
     * 考勤组: 获取当前登录用户考勤组
     * 接口路径：GET/atte/getAttendGroupByUserId
     */
    @GetMapping("getAttendGroupByUserId")
    @ApiOperation("考勤组: 获取当前登录用户考勤组")
    public Result<AttendGroupDTO> getAttendGroupByUserId(){
        AttendGroupDTO attendGroupDTO = attendGroupService.getAttendGroupByUserId();
        return Result.success(attendGroupDTO);
    }

    /**考勤组：删除
     * /localhost:10010/atte/attendGroup/11*/
    @ApiOperation("考勤组：删除")
    @DeleteMapping("attendGroup/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        attendGroupService.deleteById(id);
        return Result.success();
    }


    /**
     * 分页查询加班规则
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("rule/overtime")
    @ApiOperation("分页查询加班规则")
    public Result<PageResult<OverTimeRuleDTO>> overTime(@RequestParam(defaultValue = "1") Long page,
                                                        @RequestParam(defaultValue = "5") Long pageSize) {

        PageResult<OverTimeRuleDTO> overTimeDTOPageResult = attendGroupService.overTime(page, pageSize);

        return Result.success(overTimeDTOPageResult);
    }

    /**
     * 删除加班规则
     *
     * @param id
     */
    @DeleteMapping("rule/overtime/{id}")
    @ApiOperation("删除加班规则")
    public void deleteOvertimeRule(@PathVariable("id") Long id) {
        attendGroupService.deleteOvertimeRule(id);
    }
    /**
     * 新增加班规则
     * @param overTimeRuleDTO
     * @return
     */
    @PostMapping(value = "rule/overtime")
    @ApiOperation("新增加班规则")
    public Result addOvertimeRule(@RequestBody OverTimeRuleDTO overTimeRuleDTO) {
        attendGroupService.addOvertimeRule(overTimeRuleDTO);
        return Result.success();
    }

    /**
     * 分页查询补卡规则
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("rule/makeup")
    @ApiOperation("分页查询补卡规则")
    public Result<PageResult<MakeupRuleDTO>> makeupRule(@RequestParam(defaultValue = "1") Long page,
                                                        @RequestParam(defaultValue = "5") Long pageSize) {
        PageResult<MakeupRuleDTO> makeupRule = attendGroupService.makeupRule(page, pageSize);
        return Result.success(makeupRule);

    }

    @ApiOperation("考勤组：编辑考勤组")
    @GetMapping("/attendGroup/{id}")
    public Result<AttendGroupDTO> getAttendGroupInfo(@PathVariable("id") Integer id){
        AttendGroupDTO attendGroupDTO = attendGroupService.getAttendGroupInfo(id);
        return Result.success(attendGroupDTO);
    }

    /**
     * 编辑补卡规则
     * @param map
     * @return
     */
    @PutMapping("rule/makeup/{id}")
    @ApiOperation("编辑补卡规则")
    public Result updateMakeup( @RequestBody Map<String, String> map) {
        Long id = Long.valueOf(map.get("id"));
        String allowMakeupStr = map.get("allowMakeup");
        Integer allowMakeup = 0;
        if("true".equals(allowMakeupStr)){
            allowMakeup = 1;
        }
        Integer currentMonthOnly = 0;
        String currentMonthOnlyStr = map.get("currentMonthOnly") ;
        if("true".equals(currentMonthOnlyStr)){
            currentMonthOnly = 1;
        }
        String maxMakeupNumber = map.get("maxMakeupNumber");

        AtCompanyCommonSetting atCompanyCommonSetting = new AtCompanyCommonSetting();
        atCompanyCommonSetting.setAllowMakeup(allowMakeup);
        atCompanyCommonSetting.setMaxMakeupNumber(maxMakeupNumber);
        atCompanyCommonSetting.setCurrentMonthOnly(currentMonthOnly);
        atCompanyCommonSetting.setId(id);
        attendGroupService.updateAtCompanyCommonSettingById(atCompanyCommonSetting);

        return Result.success();
    }


    @GetMapping("config")
    public Result config(){
        return Result.success();
    }

}
